<?php
/**
 * Egany_Metabox_Periodic_Discount
 *
 * Author URI: http://www.facebook.com/
 * @author 		PhongND
 * @category 	Admin
 * @package 	egany_discount_offer
 * @version     1.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}



class Egany_Metabox_Periodic_Discount {


	/**
	 * Output the metabox 
	 */
	public static function output( $post ) {
		// global $theorder;

		// if ( ! is_object( $theorder ) ) {
			// $theorder = wc_get_order( $post->ID );
		// }

		// $order = $theorder;
		
		// wp_nonce_field( 'woocommerce_save_data', 'woocommerce_meta_nonce' );
		
		
		$args = array(
			'post_type'        => 'periodic_discount',
			'post_status'      => 'any',
			'orderby'          => 'post_date', // post_date / date
			'order'            => 'desc',
		);
		$arrPosts = get_posts( $args );
		
		if (count($arrPosts) > 0) 
		{
			$strID = $arrPosts[0]->ID;
			var_dump('Last ID: '. $strID);
			var_dump(get_post_meta( $strID, 'periodic_no', true ));
		}
		else
			var_dump('NONE');
		?>
		<style type="text/css">
		
			/*  #edit-slug-box, #minor-publishing-actions, #major-publishing-actions, #minor-publishing-actions, #visibility, #submitdiv */
			
			#post-body-content, #titlediv { display:none }
		</style>
		
		<div class="panel-wrap periodic_discount_tabs">
			<ul class="wc-tabs"">
				<?php
					$periodic_discount_tabs = apply_filters( 'woocommerce_periodic_discount_tabs', array(
						'tad_general' => array(
							'label'  => __( 'General', 'EGANY' ),
							'target' => 'tab_general_periodic_discount_data',
							'class'  => 'general_periodic_discount_data',
						),
						'tab_triggers' => array(
							'label'  => __( 'Triggers', 'EGANY' ),
							'target' => 'tab_triggers_periodic_discount_data',
							'class'  => '',
						),
						'tab_periodic_trigger' => array(
							'label'  => __( 'Periodic Trigger', 'EGANY' ),
							'target' => 'tab_periodic_trigger_periodic_discount_data',
							'class'  => '',
						),
						'tab_benefits' => array(
							'label'  => __( 'Benefits', 'EGANY' ),
							'target' => 'tab_benefits_periodic_discount_data',
							'class'  => '',
						),
						'tab_block' => array(
							'label'  => __( 'Block', 'EGANY' ),
							'target' => 'tab_block_periodic_discount_data',
							'class'  => '',
						),
						'tab_statistics' => array(
							'label'  => __( 'Statistics', 'EGANY' ),
							'target' => 'tab_statistics_periodic_discount_data',
							'class'  => '',
						)
					) );

					foreach ( $periodic_discount_tabs as $key => $tab ) {
						?><li class="<?php echo $key; ?>_options <?php echo $key; ?>_tab <?php echo implode( ' ' , (array) $tab['class'] ); ?>">
							<a href="#<?php echo $tab['target']; ?>"><?php echo esc_html( $tab['label'] ); ?></a>
						</li><?php
					}
				?>
			</ul>
			<div id="tab_general_periodic_discount_data" class="panel woocommerce_options_panel" style="">
				<?php 
					//echo '<div class="options_group">';
					
					woocommerce_wp_text_input( array( 'id' => 'periodic_no', 'label' => __( 'No', 'EGANY' ), 'placeholder' => __('Periodic No', 'placeholder', 'EGANY' ), 'description' => __( 'Periodic No.', 'EGANY' ), 'type' => 'texr', 'desc_tip' => true, 'class' => 'short' ) );
					woocommerce_wp_text_input( array( 'id' => 'description', 'label' => __( 'Description', 'EGANY' ), 'placeholder' => __('Description', 'placeholder', 'EGANY' ), 'description' => __( 'Description.', 'EGANY' ), 'type' => 'text', 'desc_tip' => true, 'class' => 'short') );
					woocommerce_wp_text_input( array( 'id' => 'priority', 'label' => __( 'Priority', 'EGANY' ), 'placeholder' => __('Priority', 'placeholder', 'EGANY' ), 'description' => __( 'Priority.', 'EGANY' ), 'type' => 'number', 'desc_tip' => true, 'class' => 'short', 'custom_attributes' => array(
							'step' 	=> '1',
							'min'	=> '0'
					)));
					// Modified date
					woocommerce_wp_text_input( array( 'id' => 'modified_date', 'label' => __( 'Modified Date', 'EGANY' ), 'description' => '', 'placeholder' => __( 'YYYY-MM-DD', 'placeholder', 'EGANY' ), 'class' => 'date-picker', 'custom_attributes' => array( 'pattern' => "[0-9]{4}-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01])" ) ) );
					
					// Enable					
					woocommerce_wp_checkbox( array( 'id' => 'enable', 'label' => __( 'Enable', 'EGANY' ), 'description' => __( 'Enable/Disable this Periodic Discount.', 'EGANY' ) ) );
					
					//echo '</div>';
					?>
				
			</div>
			<div id="tab_triggers_periodic_discount_data" class="panel woocommerce_options_panel" style="display:none;">
				<?php 
				// currency_code
				woocommerce_wp_text_input( array( 'id' => 'currency_code', 'label' => __( 'Currency Code', 'EGANY' ), 'placeholder' => __('Currency Code', 'EGANY' ), 'description' => __( 'Currency Code.', 'EGANY' ), 'data_type' => 'text', 'desc_tip' => true ) );
				// customer_group
				woocommerce_wp_text_input( array( 'id' => 'customer_group', 'label' => __( 'Customer Group', 'EGANY' ), 'placeholder' => __('Customer Group', 'EGANY' ), 'description' => __( 'Customer Group.', 'EGANY' ), 'data_type' => 'text', 'desc_tip' => true ) );
				// min_amount_to_trigger
				woocommerce_wp_text_input( array( 'id' => 'min_amount_to_trigger', 'label' => __( 'Min Amount to Trigger', 'EGANY' ), 'placeholder' => wc_format_localized_price( 0 ), 'description' => __( 'Min Amount to Trigger.', 'EGANY' ), 'data_type' => 'price', 'desc_tip' => true ) );
				
				?>
			</div>
			
			<div id="tab_periodic_trigger_periodic_discount_data" class="panel woocommerce_options_panel">
				<?php 
				
				// validation_period_id
				woocommerce_wp_text_input( array( 'id' => 'validation_period_id', 'label' => __( 'Validation Period ID', 'EGANY' ), 'placeholder' => __('Validation Period ID', 'EGANY' ), 'description' => __( 'Validation Period ID.', 'EGANY' ), 'data_type' => 'text', 'desc_tip' => true ) );
				
				// validation_description
				woocommerce_wp_text_input( array( 'id' => 'validation_description', 'label' => __( 'Validation Description', 'EGANY' ), 'placeholder' => __('Validation Description', 'EGANY' ), 'description' => __( 'Validation Description.', 'EGANY' ), 'data_type' => 'text', 'desc_tip' => true ) );
			
				// Starting Date
				woocommerce_wp_text_input( array( 'id' => 'starting_date', 'label' => __( 'Starting Date', 'EGANY' ), 'description' => '', 'placeholder' => __( 'YYYY-MM-DD', 'placeholder', 'EGANY' ), 'class' => 'date-picker', 'custom_attributes' => array( 'pattern' => "[0-9]{4}-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01])" ) ) );			
				// Ending Date
				woocommerce_wp_text_input( array( 'id' => 'ending_date', 'label' => __( 'Ending Date', 'EGANY' ), 'description' =>'', 'placeholder' => __( 'YYYY-MM-DD', 'placeholder', 'EGANY' ), 'class' => 'date-picker', 'custom_attributes' => array( 'pattern' => "[0-9]{4}-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01])" ) ) );
				
				?>
				
			</div>
			
			<div id="tab_benefits_periodic_discount_data" class="panel woocommerce_options_panel">
				<?php 
				
				// Discount Percent Value
				woocommerce_wp_text_input( array( 'id' => 'discount_percent', 'label' => __( 'Discount Percent Value', 'EGANY' ), 'placeholder' => wc_format_localized_price( 0 ), 'description' => __( 'Discount Percent Value.', 'EGANY' ), 'data_type' => 'price', 'desc_tip' => true ) );
				
				// Discount Amount Value
				woocommerce_wp_text_input( array( 'id' => 'discount_amount', 'label' => __( 'Discount Amount Value', 'EGANY' ), 'placeholder' => wc_format_localized_price( 0 ), 'description' => __( 'Discount Amount Value.', 'EGANY' ), 'data_type' => 'price', 'desc_tip' => true ) );
				

				?>
				
			</div>
			
			<div id="tab_block_periodic_discount_data" class="panel woocommerce_options_panel">
				<?php 
					// Block Line Discount					
					woocommerce_wp_checkbox( array( 'id' => 'block_line_discount', 'label' => __( 'Block Line Discount', 'EGANY' ), 'description' => __( 'Block Line Discount/Disable this Periodic Discount.', 'EGANY' ) ) );
				
					// Block Infocode Discount					
					woocommerce_wp_checkbox( array( 'id' => 'block_infocode_discount', 'label' => __( 'Block Infocode Discount', 'EGANY' ), 'description' => __( 'Block Infocode Discount/Disable this Periodic Discount.', 'EGANY' ) ) );
				
					// Block Total Discount					
					woocommerce_wp_checkbox( array( 'id' => 'block_total_discount', 'label' => __( 'Block Total Discount', 'EGANY' ), 'description' => __( 'Block Total Discount/Disable this Periodic Discount.', 'EGANY' ) ) );					
				?>
				
			</div>
			
			<div id="tab_statistics_periodic_discount_data" class="panel woocommerce_options_panel">
				<?php 
					// sales_qty
					woocommerce_wp_text_input( array( 'id' => 'sales_qty', 'label' => __( 'Sales Qty', 'EGANY' ), 'placeholder' => __('Sales Qty', 'EGANY' ), 'description' => __( 'Sales Qty.', 'EGANY' ), 'data_type' => 'text', 'desc_tip' => true ) );
					// sales_lcy
					woocommerce_wp_text_input( array( 'id' => 'sales_lcy', 'label' => __( 'Sales LCY', 'EGANY' ), 'placeholder' => __('Sales LCY', 'EGANY' ), 'description' => __( 'Sales LCY.', 'EGANY' ), 'data_type' => 'text', 'desc_tip' => true ) );
					// profit_lcy
					woocommerce_wp_text_input( array( 'id' => 'profit_lcy', 'label' => __( 'Profit LCY', 'EGANY' ), 'placeholder' => __('Profit LCY', 'EGANY' ), 'description' => __( 'Profit LCY.', 'EGANY' ), 'data_type' => 'text', 'desc_tip' => true ) );					// profit_percent
					woocommerce_wp_text_input( array( 'id' => 'profit_percent', 'label' => __( 'Profit Percent', 'EGANY' ), 'placeholder' => __('Profit Percent', 'EGANY' ), 'description' => __( 'Profit Percent.', 'EGANY' ), 'data_type' => 'text', 'desc_tip' => true ) );
				?>
				
			</div>
		</div>
		
		
		<script type="text/javascript">
			
			//ok, displaying div under a tab view.
			//asd==$, also ok!
			jQuery(function($) {
				$( ".periodic_discount_tabs" ).tabs();
			});
		</script>
		
		<?php

	}

	/**
	 * Save meta box data
	 */
	public static function save( $post_id, $post ) {
		global $wpdb;
		
		$periodic_no  	=  $_POST['periodic_no'];
		$description   	=  $_POST['description'];
		$priority   	= empty( $_POST['priority'] ) ? '' : absint( $_POST['priority']);
		$enable         = isset( $_POST['enable'] ) ? 'yes' : 'no';
		
		$modified_date         = $_POST['modified_date'];
		$currency_code         = $_POST['currency_code'];
		$customer_group         	= $_POST['customer_group'];
		$min_amount_to_trigger      = $_POST['min_amount_to_trigger']; // nono wc_format_decimal($_POST['min_amount_to_trigger']);
		$validation_period_id       = $_POST['validation_period_id'];
		$validation_description     = $_POST['validation_description'];
		$starting_date         		= $_POST['starting_date'];
		$ending_date         		= $_POST['ending_date'];
		$discount_percent        	= $_POST['discount_percent'];
		$discount_amount  			= $_POST['discount_amount'];
		$block_line_discount		= isset( $_POST['block_line_discount'] ) ? 'yes' : 'no';
		$block_infocode_discount	= isset( $_POST['block_infocode_discount'] ) ? 'yes' : 'no';
		$block_total_discount		= isset( $_POST['block_total_discount'] ) ? 'yes' : 'no';
		$sales_qty         	= $_POST['sales_qty'];
		$sales_lcy         	= $_POST['sales_lcy'];
		$profit_lcy         = $_POST['profit_lcy'];
		$profit_percent    	= $_POST['profit_percent'];
		
		
		// Update meta
		update_post_meta( $post_id, 'periodic_no', $periodic_no);
		update_post_meta( $post_id, 'description', $description);
		update_post_meta( $post_id, 'priority', $priority);
		update_post_meta( $post_id, 'enable', $enable);
		update_post_meta( $post_id, 'modified_date', $modified_date);
		update_post_meta( $post_id, 'currency_code', $currency_code);
		update_post_meta( $post_id, 'customer_group', $customer_group);
		update_post_meta( $post_id, 'min_amount_to_trigger', $min_amount_to_trigger);
		update_post_meta( $post_id, 'validation_period_id', $validation_period_id);
		update_post_meta( $post_id, 'validation_description', $validation_description);
		update_post_meta( $post_id, 'starting_date', $starting_date);
		update_post_meta( $post_id, 'ending_date', $ending_date);
		update_post_meta( $post_id, 'discount_percent', $discount_percent);
		update_post_meta( $post_id, 'discount_amount', $discount_amount);
		update_post_meta( $post_id, 'block_line_discount', $block_line_discount);
		update_post_meta( $post_id, 'block_infocode_discount', $block_infocode_discount);
		update_post_meta( $post_id, 'block_total_discount', $block_total_discount);
		//_tab_statistic - no need to save
		update_post_meta( $post_id, 'sales_qty', $sales_qty);
		update_post_meta( $post_id, 'sales_lcy', $sales_lcy);
		update_post_meta( $post_id, 'profit_lcy', $profit_lcy);
		update_post_meta( $post_id, 'profit_percent', $profit_percent);
		
		
		return $post_id;
	}

}


